package com.manes.agents;

import org.matheclipse.core.eval.EvalUtilities;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.form.output.OutputFormFactory;
import org.matheclipse.core.form.output.StringBufferWriter;
import org.matheclipse.core.interfaces.IExpr;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
//import jade.gui.GuiAgent;
//import jade.gui.GuiEvent;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.util.leap.Iterator;

import edu.jas.kern.ComputerThreads;

public class mathAgent extends Agent {
	/**
	 * 
	 */
	
	private static final long serialVersionUID = 1L;
	private MessageTemplate template = MessageTemplate.and(
			MessageTemplate.MatchPerformative(ACLMessage.INFORM),
			MessageTemplate.MatchOntology("mnsOnto") );
	protected ACLMessage lastReceivedMsg;
	
	
    EvalUtilities util = new EvalUtilities();

    IExpr result;
	
	
	protected void setup(){
		F.initSymbols(null);
		System.out.println("MathAgent agent online!");
		System.out.println(" local-name  "+getAID().getLocalName());
		System.out.println("GUID "+getAID().getName());
		System.out.println("address: ");
		Iterator it = getAID().getAllAddresses();
		while(it.hasNext()){
			System.out.println(" - "+it.next());
		}
		
		ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
		msg.addReceiver(new AID("Manes", AID.ISLOCALNAME));
		msg.setLanguage("English");
		msg.setOntology("mnsOnto");
		msg.setContent("MathAgent agent online!");
		send(msg);
		
		 addBehaviour(new CyclicBehaviour(this) {
				public void action() {
					ACLMessage msg = myAgent.receive(template);
					
					
					if (msg != null) {
	                                        setReceivedMsg(msg);
	                                    lastReceivedMsg = msg;
	                                    doReply();
					}
					else {
						block();
					}
				}

				private void setReceivedMsg(ACLMessage msg) {
					// TODO Auto-generated method stub
					int s = msg.getPerformative();
					 String recieveContent=msg.getContent();
					 System.out.println(s);
					
				}
				private void doReply() {
			        
					try {
						ACLMessage reply = lastReceivedMsg.createReply();
						
						 String rData = lastReceivedMsg.getContent();
							

					      StringBufferWriter buf = new StringBufferWriter();
					    // String input = "Expand[(AX^2+BX)^2]";
					      result = util.evaluate(rData);
					      OutputFormFactory.get().convert(buf, result);
					      String output = buf.toString();
					     //System.out.println("Expanded form for " + input + " is " + output);
					      reply.setPerformative(ACLMessage.INFORM);
							reply.setContent(output);
							
				        send(reply);
					      
					    } catch (final Exception e) {
					      e.printStackTrace();
					    } finally {
					      // Call terminate() only one time at the end of the program  
					      ComputerThreads.terminate();
					    }
					 			    }
			} );
		 
	}
		
	
	
}
